package com.molyfun.parents.fragment.main;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.classroom.TeamClassListActivity;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.activity.mineCourse.CourseCategoryActivity;
import com.molyfun.parents.activity.setting.MineActivity;
import com.molyfun.parents.adapter.MineCourseAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.TwoTabBar;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.lang.reflect.Field;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by ZhaoWei on 2016/12/5.
 * 首页
 */

public class MineCourseFragment extends BaseFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener, MineCourseAdapter.MineCourseListInterface {


    RecyclerView mRecyclerView;
    SwipeRefreshLayout mSwipeRefreshLayout;

    private LinearLayoutManager mLayoutManager;
    private MineCourseAdapter mProductAdapter;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    private int mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;

    String todayDate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_mine_class, container, false);
        ButterKnife.inject(this, view);

        mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipe_refresh_layout);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mProductAdapter = new MineCourseAdapter(getContext(), this);
        mRecyclerView.setAdapter(mProductAdapter);

        todayDate = TimeUtils.getQueryDate();

        try {
            Field f = null;
            f = SwipeRefreshLayout.class.getDeclaredField("mTouchSlop");
            f.setAccessible(true);
            f.set(mSwipeRefreshLayout, 150);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        loadData(RestClient.DEFAULT_PAGE, RestClient.DEFAULT_PAGE_SIZE, true);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.add_class:
//                onClassAdd();
//                break;
        }
    }

    @Override
    public void onTeamClassClick() {
        TeamClassListActivity.startActivity(getContext());
    }

    @Override
    public void onClassAdd() {
        System.out.println("onClassAdd---->");
        Message uimessage = new Message();
        uimessage.what = EventDispatcherEnum.UI_EVENT_CHANGE_TAB;
        uimessage.arg1 = R.id.rl_tab_class_center;
        MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
    }

    @Override
    public void onTwoBarClick(int chooseTab) {
        switch (chooseTab){
            case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
                break;
            case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_TWO;
                break;
        }
        onRefresh();
    }

    @Override
    public void onDiscountsClick() {
        CourseCategoryActivity.startActivity(getContext(), CourseCategoryActivity.COURSE_CATEGORY_TYPE_BEST);
    }

    @Override
    public void onHistoryClick() {
        CourseCategoryActivity.startActivity(getContext(), CourseCategoryActivity.COURSE_CATEGORY_TYPE_HISTORY);
    }

    @Override
    public void onProfileBtnClick() {
        MineActivity.startActivity(getContext());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    private void loadData(int pageNum, int pageSize, final boolean isRefresh) {

        if(isRefresh){
            mCurrentPage = RestClient.DEFAULT_PAGE;
        }
        showProgressDialog();
        Call<SubjectListResponse> call = MFPApplication.getInstance().getRestClient().getMySubjectList(pageNum, pageSize);
        call.enqueue(new Callback<SubjectListResponse>() {
            @Override
            public void onResponse(Call<SubjectListResponse> call, Response<SubjectListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {

                    if(isRefresh){
                        if(response.body().getData().size() > 0){
//                            mAddClass.setVisibility(View.GONE);
                            mProductAdapter.refreshData(response.body().getData());
                            mHasMoreData = true;
                        }else{
//                            mAddClass.setVisibility(View.VISIBLE);
                            mHasMoreData = false;
                            mProductAdapter.clearAll();
                        }
                    }else{
                        if(response.body().getData().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        mProductAdapter.addAll(response.body().getData());
                    }

                }else{
//                    mAddClass.setVisibility(View.VISIBLE);
                    mHasMoreData = false;
                    mProductAdapter.clearAll();
                }
            }

            @Override
            public void onFailure(Call<SubjectListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void getClassCourseList(final String date, final boolean isRefresh) {
//        mAddClass.setVisibility(View.GONE);
        showProgressDialog();
        Call<CourseListResponse> call = MFPApplication.getInstance().getRestClient().getUserCourseList(date, mCurrentPage);
        call.enqueue(new Callback<CourseListResponse>() {
            @Override
            public void onResponse(Call<CourseListResponse> call, Response<CourseListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData().getRecords() != null) {
                    if(isRefresh){
                        if(response.body().getData().getRecords().size() > 0){
                            if(response.body().getData().getRecords().size() > 2){
                                mProductAdapter.refreshClassCourseData(response.body().getData().getRecords().subList(0, 2));
                            }else{
                                mProductAdapter.refreshClassCourseData(response.body().getData().getRecords());
                            }
                            mHasMoreData = false;
                        }else{
                            mHasMoreData = false;
                            mProductAdapter.clearAllClassCourse();
                        }
                    }else{
                        if(response.body().getData().getRecords().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        mProductAdapter.addAllClassCourse(response.body().getData().getRecords());
                    }

                }else{
                    mHasMoreData = false;
                    mProductAdapter.clearAllClassCourse();
                }
            }

            @Override
            public void onFailure(Call<CourseListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        switch (mCurrentTab){
            case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                loadData(RestClient.DEFAULT_PAGE, RestClient.DEFAULT_PAGE_SIZE, true);
                break;
            case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                getClassCourseList(todayDate, true);
                break;
        }

        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
    }

    @Override
    public void onLoad() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        if(mHasMoreData){
            mCurrentPage ++;

            switch (mCurrentTab){
                case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                    loadData(RestClient.DEFAULT_PAGE, RestClient.DEFAULT_PAGE_SIZE, true);
                    break;
                case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                    getClassCourseList(todayDate, true);
                    break;
            }

        }
    }

}
